package com.thkj.controller;

import com.thkj.entity.TblCameraRele;
import com.thkj.entity.TblIndividual;
import com.thkj.service.TblIndividualService;
import com.thkj.service.TblMoniConfigService;
import com.thkj.camera.CamerasRequest;
import com.thkj.camera.HikCamerasResoures;
import com.thkj.util.RequestUtil;
import dp.R;
import dp.dao.PagingList;
import dp.util.DateUtil;
import com.thkj.framework.base.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 经营户Action
 *
 * @author weihuu
 */
@RestController
@RequestMapping("/backstage/noniconfig")
public class MoniconfigAction extends BaseController {

    @Autowired
    TblIndividualService tblIndividualService;

    @Autowired
    TblMoniConfigService moniConfigService;

    /**
     * 经营户列表
     *
     * @return
     */
    @RequestMapping("")
    public ModelAndView index() {
        return new ModelAndView("noniconfig/list");
    }

    @RequestMapping("/query")
    public R query(HttpServletRequest request, int page, int size) {
        Map<String, Object> param = RequestUtil.getParameterMap(request);
        PagingList<Map> list = tblIndividualService.query(page, size, param);
        return R.success(list);
    }

    /**
     * 获取组织
     */
    @RequestMapping("/cameraOrigin")
    public R cameraOrigin(HttpServletRequest request) {
        List<Map> hikOrigin = HikCamerasResoures.getHikOrigin();
        return R.success(hikOrigin);
    }


    @RequestMapping("/bindCamera")
    @Transactional
    public R bindCamera(@RequestParam(value = "indexCode") String  indexCode ,
                        @RequestParam(value = "name") String name ,
                        @RequestParam(value = "individualId") String  individualId) {
        List<TblCameraRele> tblCameraReles = moniConfigService.getByIndexCode(indexCode);
        if (tblCameraReles != null && tblCameraReles.size() > 0) {
            TblIndividual tblIndividual = tblIndividualService.getTblIndividualById(individualId);
            return R.failure("已经被" + tblIndividual.getName() + "绑定,请先解绑!");
        }
        TblCameraRele  cameraRele  = new TblCameraRele();
        cameraRele.setIndexCode(indexCode);
        cameraRele.setName(name);
        cameraRele.setIndividualId(individualId);
        cameraRele.setCreateTime(DateUtil.getNowAsTimestamp());
        cameraRele.setManageForbidJurisdictions(null);
        cameraRele.setBusinessForbidJurisdictions(null);
        cameraRele.setUpdateTime(cameraRele.getCreateTime());
        moniConfigService.insertCameraRele(cameraRele);

        TblIndividual individual = new TblIndividual();
        individual.setCameraId(cameraRele.getId() + "");
        individual.setId(cameraRele.getIndividualId());
        individual.setUpdateTime(DateUtil.getNowAsTimestamp());
        tblIndividualService.updateCameraId(individual);
        return R.SUCCESS;
    }

    @RequestMapping("/unbindCamera")
    @Transactional
    public R unbindCamera(TblCameraRele cameraRele) {
        if (StringUtils.isBlank(cameraRele.getId() + "") || StringUtils.isBlank(cameraRele.getIndividualId())) {
            return R.failure("参数格式错误!");
        }
        moniConfigService.delete(cameraRele.getId() + "");

        TblIndividual individual = new TblIndividual();
        individual.setCameraId("");
        individual.setId(cameraRele.getIndividualId());
        individual.setUpdateTime(DateUtil.getNowAsTimestamp());
        tblIndividualService.updateCameraId(individual);
        return R.SUCCESS;
    }


    @RequestMapping("/getCameras")
    public R getCameras(HttpServletRequest request) {
        Map<String, Object> parameterMap = RequestUtil.getParameterMap(request);
        String cameraId = (String) parameterMap.get("cameraId");
        String client_type = (String) parameterMap.get("client_type");

        if (StringUtils.isBlank(cameraId) || StringUtils.isBlank(client_type)) {
            return R.failure("参数格式错误!");
        }

        TblCameraRele cameraRele = moniConfigService.getById(cameraId);
        if (cameraRele == null) {
            return R.failure("未查询到数据!");
        }

        CamerasRequest camerasRequest = new CamerasRequest();
        camerasRequest.setPageNo(1);
        camerasRequest.setPageSize(200);
        camerasRequest.setRegionIndexCode(cameraRele.getIndexCode());
        camerasRequest.setTreeCode(cameraRele.getTreeCode());
        List<Map> cameras = HikCamerasResoures.getCameras(camerasRequest);

        if ("1".equals(client_type)) { //监管端
            String manageForbidJurisdictions = cameraRele.getManageForbidJurisdictions();
            if (StringUtils.isNotBlank(manageForbidJurisdictions)) {
                List<String> ids = Arrays.asList(manageForbidJurisdictions.split(","));
                for (Map camera : cameras) {
                    if (ids.contains(camera.get("cameraIndexCode"))) {
                        camera.put("isAuth", false);
                    } else {
                        camera.put("isAuth", true);
                    }
                }
            } else {
                for (Map camera : cameras) {
                    camera.put("isAuth", true);
                }
            }
        } else if ("2".equals(client_type)) { //商户端
            String businessForbidJurisdictions = cameraRele.getBusinessForbidJurisdictions();
            if (StringUtils.isNotBlank(businessForbidJurisdictions)) {
                List<String> ids = Arrays.asList(businessForbidJurisdictions.split(","));
                for (Map camera : cameras) {
                    if (ids.contains(camera.get("cameraIndexCode"))) {
                        camera.put("isAuth", false);
                    } else {
                        camera.put("isAuth", true);
                    }
                }
            } else {
                for (Map camera : cameras) {
                    camera.put("isAuth", true);
                }
            }
        }
        return R.success(cameras);
    }


    @RequestMapping("/setJurisdiction")
    @Transactional
    public R setJurisdiction(HttpServletRequest request) {
        Map<String, Object> parameterMap = RequestUtil.getParameterMap(request);
        String cameraId = (String) parameterMap.get("cameraId");
        String client_type = (String) parameterMap.get("client_type");
        String camerasIds = (String) parameterMap.get("camerasIds");

        if (StringUtils.isBlank(cameraId) || StringUtils.isBlank(client_type)) {
            return R.failure("参数格式错误!");
        }

        TblCameraRele cameraRele = moniConfigService.getById(cameraId);
        if (cameraRele == null) {
            return R.failure("修改异常数据!");
        }

        cameraRele.setUpdateTime(DateUtil.getNowAsTimestamp());
        if ("1".equals(client_type)) { //监管端
            cameraRele.setManageForbidJurisdictions(camerasIds);
        } else if ("2".equals(client_type)) { //商户端
            cameraRele.setBusinessForbidJurisdictions(camerasIds);
        }
        moniConfigService.updateTblCameraRele(cameraRele);
        return R.SUCCESS;
    }


}